#pragma once

#include <iostream>
#include <functional>
#include <vector>
#include <ctime>
#include <unistd.h>

typedef std::function<void()> task_t;

void Download()
{
    std::cout << "我是一个下载任务" << " 处理者: " << getpid() << std::endl;
}

void PrintLog()
{
    std::cout << "我是一个打印日志的任务" << " 处理者: " << getpid() << std::endl;
}

void PushVideoStream()
{
    std::cout << "我是一个推送视频流的任务" << " 处理者: " << getpid() << std::endl;
}


class Init
{
public:
    // 任务码
    const static int g_download_code = 0;
    const static int g_printlog_code = 1;
    const static int g_push_videostream_code = 2;
    // 任务集合
    std::vector<task_t> tasks;
public:
    Init()
    {
        tasks.push_back(Download);
        tasks.push_back(PrintLog);
        tasks.push_back(PushVideoStream);

        srand(time(nullptr) ^ getpid());    // ^getpid(),让数据更有随机性
    }

    // 检查任务码是否安全
    bool CheckSafe(int code)
    {
        if (code >= 0 && code < tasks.size()) return true;
        else return false;
    }

    // 执行任务
    void RunTask(int code)
    {
        tasks[code]();
    }

    int SelectTask()
    {
        // 返回随机任务码
        return rand() % tasks.size();
    }

    // 返回任务码对应的具体任务
    std::string ToDesc(const int &code)
    {
        switch(code)
        {
            case g_download_code:
                return "Download";
            case g_printlog_code:
                return "PrintLog";
            case g_push_videostream_code:
                return "PushVideoStream";
            default:
                return "Unknow";
        }
        return "";
    }
};

Init init;  // 定义对象